package org.jgs1904.apachepoidemo.demo.sso;

/**
 * @author: 默苍璃
 * @date: 2024-01-08 17:43
 */


import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @author 乔坤
 * @since 2023-09-04 08:47:23
 */
@Slf4j
public class TestClient2 {
    public static String bucketName = "dbs-dev";
    public static MinioClient minioClient = null;

    public static void main(String[] args) throws Exception {


        // 当前时间
        long startTime = System.currentTimeMillis();
        // log.info("开始时间 " + startTime + " milliseconds.");
        log.info("开始时间 {} milliseconds.", startTime);
        minioClient = getMinioClient();
        getDownloadPath();

        t0();
        t1();
        // 计算耗时
        long endTime = System.currentTimeMillis();
        long duration = endTime - startTime;
        log.info(" 耗时 " + duration + " milliseconds.");

    }

    public static MinioClient getMinioClient() {
        String accessKey = "LYxhhbPGu5muxsCHJ9Nd";
        String secretKey = "FCI1gkFg4BAuDzg85OZOjy5QzGsOfq5HTrXCiWHQ";
        return MinioClient.builder()
                .endpoint("192.168.8.85", 9000, false)
                .credentials(accessKey, secretKey)
                .build();
    }


    static void getDownloadPath() throws ServerException, InsufficientDataException, ErrorResponseException,
            IOException,
            NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException,
            InternalException {
        String url =
                minioClient.getPresignedObjectUrl(
                        GetPresignedObjectUrlArgs.builder()
                                .method(Method.GET)
                                .bucket("dbs-dev")
                                .object("drawing/template/bug.txt")
                                .expiry(60 * 60 * 24)
                                .build());

        System.out.println(url);

    }

    public static void t0() throws Exception {
        List<Bucket> buckets = minioClient.listBuckets();
        for (Bucket bucket : buckets) {
            log.info("桶子：{}", bucket.name());
        }
    }

    public static void t1() throws Exception {
        // String filePath = "D:\\work\\图文档管理\\dwg\\dwg\\CBHPA22S8M150-A-N24.dwg";
        String filePath = "D:\\work\\图文档管理\\dwg\\dwg\\D.45.05.00240.pdf";
        // String objectName = "小鸟.dwg";
        String objectName = "小鸟.pdf";
        File file = new File(filePath);
        long length = file.length();
        FileInputStream fileInputStream = new FileInputStream(file);

        PutObjectArgs build = PutObjectArgs.builder()
                .bucket(bucketName)
                .stream(fileInputStream, length, -1)
                .object(objectName)
                .contentType("binary/octet-stream")
                .build();
        minioClient.putObject(build);
        fileInputStream.close();
        log.info("上传完成");
    }
}
